<!--  -->
<template>
  <div class="cover-container"
       @click="showCoverSelect">
    <div class="cover-wrap">
      <img class="cover-img"
           ref="cover-image"
           :src="value">
    </div>
    <el-dialog title="选择封面"
               :visible.sync="dialogVisible"
               width="60%"
               append-to-body>
      <el-tabs v-model="activeName"
               type="card">
        <el-tab-pane label="素材库"
                     name="first">
          <!-- 素材列表   -->
          <image-list :isShowUpload="false" ref="image-list"/>
        </el-tab-pane>
        <!-- 上传图片 -->
        <el-tab-pane label="上传图片"
                     name="second">
          <input type="file"
                 ref="file"
                 @change="fileChange">
          <img src=""
               width="100"
               alt=""
               ref="preview-image">
        </el-tab-pane>
      </el-tabs>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onCoverConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { uploadImages } from '@/api/images'
import ImageList from '@/views/image/components/image-list'
export default {
  name: 'UploadCover',
  components: {
    ImageList
  },
  // props: ['cover-image'],
  props: ['value'],
  data () {
    return {
      dialogVisible: false,
      activeName: 'first'
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    showCoverSelect () {
      this.dialogVisible = true
    },
    fileChange () {
      const file = this.$refs.file.files[0]
      const blob = window.URL.createObjectURL(file)
      this.$refs['preview-image'].src = blob
      // 防止用户选择同一个文件不触发事件
      // this.$refs.file.value = ''
    },
    onCoverConfirm () {
      if (this.activeName === 'second') {
        // console.log(this.$refs.file.files)
        const file = this.$refs.file.files[0]
        if (!file) {
          this.$message('请选择文件')
          return
        }
        const fd = new FormData()
        fd.append('image', file)
        // 执行上传文件操作
        uploadImages(fd).then(res => {
          console.log(res)
          // 关闭弹出层
          this.dialogVisible = false
          // 展示上传的图片
          this.$refs['cover-image'].src = res.data.data.url

          // 将图片地址发送给父组件
          this.$emit('input', res.data.data.url)
          // this.$emit('updata-cover', res.data.data.url)
        })
      } else if (this.activeName === 'first') {
        // 获取子组件的数据dom
        const imageList = this.$refs['image-list']
        const selected = imageList.selected
        if (selected === null) {
          this.$message('请选择封面图片')
        }

        this.dialogVisible = false
        console.log(imageList.images[selected])
        // 提交给父组件
        this.$emit('input', imageList.images[selected].url)
      }
    }
  }
}
</script>

<style lang='less' scoped>
.cover-wrap {
  width: 150px;
  height: 120px;
  border: 1px solid #000;
  .cover-img {
    max-width: 100%;
    height: 120px;
  }
}
</style>
